import axiosServer from '@/api/request'
import type { IRole, IResponseData } from '@/utils/types'
/**
 * 登录接口
 * @param {*} username
 * @param {*} password
 * @returns
 */
export const RequestLogin = (username: string, password: string): Promise<IResponseData> => {
	return axiosServer({
		method: 'post',
		url: '/api/login',
		data: {
			username,
			password
		}
	})
}

/**
 * get请求
 */
export const RequestDeleteProduct = (id: number): Promise<IResponseData> => {
	return axiosServer({
		method: 'get',
		url: '/api/shop/delete',
		params: {
			id
		}
	})
}

/**
 * 验证码
 */
export const RequesetVeryCode = (): Promise<IResponseData> => {
	return axiosServer({
		method: 'get',
		url: 'api/verifyCode'
	})
}

/**
 * 动态菜单
 */
export const RequestMenu = (roleId: number): Promise<IResponseData> => {
	return axiosServer({
		method: 'get',
		url: '/api/system/menus',
		params: {
			roleId
		}
	})
}
/**
 * 子菜单列表
 */
export const RequestSubMenu = (menuId: number): Promise<IResponseData> => {
	return axiosServer({
		method: 'get',
		url: '/api/system/submenu',
		params: {
			menuId
		}
	})
}

/**
 * 添加菜单
 */
export const RequestMenuAdd = (menu: any): Promise<IResponseData> => {
	const { parentId, nodeType, menuName, path, icon } = menu
	return axiosServer({
		method: 'post',
		url: '/api/system/addmenu',
		data: {
			parentId,
			nodeType,
			menuName,
			path,
			icon
		}
	})
}

/**
 * 编辑菜单
 */
export const RequestMenuEdit = (menu: any): Promise<IResponseData> => {
	const { id, menuName, path, icon } = menu
	return axiosServer({
		method: 'post',
		url: '/api/system/editmenu',
		data: {
			id,
			menuName,
			path,
			icon
		}
	})
}

/**
 * 角色列表
 */
export const RequestRoleList = (): Promise<IResponseData> => {
	return axiosServer({
		method: 'get',
		url: '/api/system/roles'
	})
}

/**
 * 添加角色
 */
export const RequestRoleAdd = (role: IRole): Promise<IResponseData> => {
	return axiosServer({
		method: 'post',
		url: '/api/system/addrole',
		data: {
			roleName: role.rolename,
			description: role.description
		}
	})
}

/**
 * 编辑角色
 */
export const RequestRoleEdit = (role: IRole): Promise<IResponseData> => {
	return axiosServer({
		method: 'post',
		url: '/api/system/editrole',
		data: {
			roleId: role.r_id,
			roleName: role.rolename,
			description: role.description
		}
	})
}

/**
 * 删除角色
 */
export const RequestRoleDelete = (roleId: number): Promise<IResponseData> => {
	return axiosServer({
		method: 'delete',
		url: '/api/system/deleterole',
		params: {
			roleId
		}
	})
}

/**
 * 权限设置
 * ids:'1,3,4,5,6'
 * resType: 0菜单资源, 2按钮资源
 */
export const RequestAuthorRole = (roleId: number, ids: string, resType: number): Promise<IResponseData> => {
	return axiosServer({
		method: 'put',
		url: '/api/system/author',
		params: {
			roleId,
			ids,
			resType
		}
	})
}

/**
 * 账户列表
 * @returns
 */
export const RequestAccountList = ({ pageNo = 1, pageSize = 5, username = '' }): Promise<IResponseData> => {
	return axiosServer({
		url: '/api/manager/list',
		method: 'get',
		params: {
			pageNo,
			pageSize,
			username
		}
	})
}

/**
 * 注册-带头像上传
 */
export const RequestRegister = (formData: FormData): Promise<IResponseData> => {
	return axiosServer({
		method: 'post',
		url: '/api/register',
		headers: { 'Content-Type': 'multipart/form-data' },
		data: formData
	})
}

/**
 * 删除账户
 */
export const RequestManagerDelete = (id: number): Promise<IResponseData> => {
	return axiosServer({
		method: 'get',
		url: '/api/manager/delete',
		params: {
			id
		}
	})
}

/**
 * 资源权限
 */
export const RequestResource = (roleId: number): Promise<IResponseData> => {
	return axiosServer({
		method: 'get',
		url: '/api/system/resource',
		params: {
			roleId
		}
	})
}

/**
 * 刷新token
 */
export const RequestRefreshToken = (longToken: string): Promise<IResponseData> => {
	return axiosServer({
		method: 'get',
		url: '/api/refresh',
		params: {
			longToken
		}
	})
}

/**
 * 商家列表
 */
export const RequestStoreList = () => {
	return axiosServer({
		method: 'get',
		url: '/api/store'
	})
}

/**
 * 删除菜单
 */
export const RequestMenuDelete = (roleId: number, id: number) => {
	return axiosServer({
		method: 'delete',
		url: '/api/system/menu',
		params: {
			roleId,
			id
		}
	})
}

/**
 * 子菜单列表
 */
export const RequesetSubMenu = (menuId: number) => {
	return axiosServer({
		method: 'get',
		url: '/api/system/submenu',
		params: {
			menuId
		}
	})
}

/**
 * 动态菜单
 */
export const RequestMenuAll = () => {
	return axiosServer({
		method: 'get',
		url: '/api/system/menuall'
	})
}
/**
 * 动态菜单
 */
export const RequestMenuAuthAll = (roleId: number) => {
	return axiosServer({
		method: 'get',
		url: '/api/system/menuauth',
		params: {
			roleId
		}
	})
}

export const indexApi = {
	RequestLogin,
	RequestRegister,
	RequesetVeryCode,
	RequestManagerDelete,

	RequestDeleteProduct,
	RequestStoreList,

	RequestMenu,
	RequestMenuAll,
	RequestMenuAuthAll,
	RequestSubMenu,
	RequesetSubMenu,
	RequestMenuAdd,
	RequestMenuEdit,
	RequestMenuDelete,
	
	RequestRoleList,
	RequestRoleAdd,
	RequestRoleEdit,
	RequestRoleDelete,
	RequestAuthorRole,
	RequestAccountList,
	RequestResource,
	RequestRefreshToken
}
